﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APPLICATION.Departments
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tIMSDataSet1.dtDep' table. You can move, or remove it, as needed.
            //this.dtDepTableAdapter.Fill(this.tIMSDataSet1.dtDep);
            this.sqlConnection1.ConnectionString = globals.APPCONNSTRING;
            //this.sqlConnection1.ConnectionString = "Data Source=(local);Initial Catalog=TIMS;Persist Security Info=True;User ID=sa;Password=123";
            //this.sqlDataAdapter1.Fill(this.tIMSDataSet.dtDep);
            this.sqlDataAdapter1.Fill(this.TIMSDataSet1.dtDep);
            //this.tsp_dtDepTableAdapter.Fill(this.tIMSDataSet.tsp_dtDep);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int intID= Convert.ToInt32((object)this.treeList1.Selection[0].GetValue("CodeID"));

            string strName= this.treeList1.Selection[0].GetValue("hName").ToString();
            
            frmDelete frmDel = new frmDelete(strName);
            if (frmDel.ShowDialog() == DialogResult.OK)
            {
                //----------------------------------//
                //    PERFORM DELETING PROCESS      //
                //----------------------------------//
                this.Enabled = false;

                System.Data.SqlClient.SqlCommand cmdSQL = new System.Data.SqlClient.SqlCommand();
                System.Data.SqlClient.SqlConnection cnSQL = new System.Data.SqlClient.SqlConnection();
                cnSQL.ConnectionString = globals.APPCONNSTRING;

                cmdSQL.CommandType = CommandType.StoredProcedure;
                cmdSQL.CommandText = "tsp_dtDep";
                cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 2;
                cmdSQL.Parameters.Add("@CodeID", SqlDbType.Int).Value = intID;

                try
                {
                    cnSQL.Open();
                    cmdSQL.Connection = cnSQL;
                    cmdSQL.ExecuteNonQuery();
                    cnSQL.Close();
                    btnRefresh_Click(sender, e);
                    this.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Произошла ошибка во время удаления выбранного элемента! \n"+ex.Message, "TIMS");
                    this.Enabled = true;
                }
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.TIMSDataSet1.dtDep.Clear();
            this.sqlDataAdapter1.Fill(this.TIMSDataSet1.dtDep);
            this.treeList1.ExpandAll();
            //this.tIMSDataSet.dtDep.Clear();
            //this.sqlDataAdapter1.Fill(this.tIMSDataSet.dtDep);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string strParentName, strName;
            int intMgrID, intCodeID;
            
            strParentName = "";
            intMgrID = 0;
            if (this.treeList1.Selection[0].Level!=0)
            {
                strParentName = this.treeList1.Selection[0].ParentNode.GetValue("hName").ToString();
                intMgrID = Convert.ToInt32(treeList1.Selection[0].GetValue("mgrid"));
            }
            
            intCodeID = Convert.ToInt32(treeList1.Selection[0].GetValue("CodeID"));

            strName = treeList1.Selection[0].GetValue("hName").ToString();

            frmEdit frmEd = new frmEdit(strParentName, strName, intMgrID, intCodeID);
            
            if (frmEd.ShowDialog(this) == DialogResult.OK)
            {
                btnRefresh_Click(sender, e);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string strParentName;
            int intMgrID;

            strParentName = "";
            //intMgrID = 0;

            if (treeList1.Selection[0] != null)
            {
                strParentName = treeList1.Selection[0].GetValue("hName").ToString();
                intMgrID = Convert.ToInt32(treeList1.Selection[0].GetValue("CodeID"));
        
            }else
            {
                strParentName = "";
                intMgrID = 0;
            }
        

            frmADD frmAddNew = new frmADD(intMgrID, strParentName);

            if (frmAddNew.ShowDialog(this) == DialogResult.OK)
            {
                btnRefresh_Click(sender, e);
            }
        }
   }
}
